﻿#pragma once
#include <map>
#include <vector>
#include <string>
#include <string.h>
#include <memory>
#include <iostream>
#include "ThostFtdcUserApiStruct.h"


#ifdef _MSC_VER
#pragma comment(lib, "libcurl_imp.lib")
#pragma comment(lib, "thostmduserapi_se.lib")
#pragma comment(lib, "thosttraderapi_se.lib")
#endif

#if defined(TRMANAGER_LIBRARY)
#  define TRMANAGERSHARED_EXPORT __declspec(dllexport)
#else
#  define TRMANAGERSHARED_EXPORT __declspec(dllimport)
#pragma comment(lib, "TrManager.lib")
#endif



#define MAX_INSTRUMENT_LENGTH	32
#define MAX_EXCHANGE_LENGTH		16

enum class TrResponseID
{
    TrOnRspUserLogin = 101,
    TrOnRspUserLogout = 102,
    TrOnRspUserLoginFailed = 103,
    TrOnRspQryTradingAccount = 104,
    TrOnInvestorPositions = 105,
    TrOnRspQryInvestorPosition = 106,
    TrOnRtnOrder = 107,
    TrOnRtnTrade = 108,
    TrOnRspError = 109,
    TrOnRspOrderInsert = 110,
    TrOnRspQryInstrument = 111,
    TrOnQuote = 112,
    TrOnFrontConnected = 130,
    TrMarketUserLogin = 131,
    TrMarketUserLogout = 132,
    TrOnRtnDepthMarketData = 133,
    TrOnFrontDisconnected = 134
};

struct TrRtnOrderField
{
    TThostFtdcInstrumentIDType  InstrumentID;
    int                         Lots;
    double                      LimitPrice;
    int                         Offset;
    char                        source[50];
};

enum class TrUserLoginStatus
{
    OffLine = 0,
    OnLine = 1,
    Logging = 2,
    Completed = 3,
    Failed = 4
};

enum class TrInstrumentStatus
{
    NoData = 0,
    Updatting = 1,
    Successed = 2
};

enum class TrInvestorPositionStatus
{
    NoQuery = 0,
    Querying = 1,
    Ready = 2
};

enum class TrDataStyle
{
    M1 = 1,
    M5 = 5,
    M15 = 15,
    H1 = 60,
    D1 = 1440
};

typedef enum tagOptionType
{
    OT_None = 0,
    OT_Call = '1',		//看涨期权
    OT_Put = '2'		//看跌期权
} OptionType;

typedef enum tagContractCategory
{
    CC_Stock,			//股票
    CC_Future,			//期货
    CC_FutOption,		//期货期权，商品期权是这个分类
    CC_Combination,		//组合
    CC_Spot,			//即期
    CC_EFP,				//期转现
    CC_SpotOption,		//现货期权，股指期权是这个分类
    CC_ETFOption,		//个股期权，ETF期权是这个分类

    CC_DC_Spot = 20,	//数币现货
    CC_DC_Swap,			//数币永续
    CC_DC_Future,		//数币期货
    CC_DC_Margin,		//数币杠杆
    CC_DC_Option,		//数币期权

    CC_UserIndex = 90	//自定义指数
} ContractCategory;

/*
 *	平仓类型
 */
typedef enum tagCoverMode
{
    CM_OpenCover,		//开平
    CM_CoverToday,		//开平昨平今
    CM_UNFINISHED,		//平未了结的
    CM_None			//不区分开平
} CoverMode;

/*
 *	交易模式
 */
typedef enum tagTradingMode
{
    TM_Both,	//多空都支持
    TM_Long,	//只能做多
    TM_LongT1,	//做多T+1
    TM_None = 9	//不能交易
} TradingMode;

/*
*	价格模式
*/
typedef enum tagPriceMode
{
    PM_Both,		//市价限价都支持
    PM_Limit,		//只支持限价
    PM_Market,		//只支持市价
    PM_None = 9		//不支持交易
} PriceMode;

/*
 *	K线数据类型
 *	开、高、低、收、量、额、日期、时间
 */
typedef enum tagKlineFieldType
{
    KFT_OPEN,
    KFT_HIGH,
    KFT_LOW,
    KFT_CLOSE,
    KFT_DATE,
    KFT_TIME,
    KFT_VOLUME,
    KFT_SVOLUME
} WTSKlineFieldType;

/*
 *	K线周期
 */
typedef enum tagKlinePeriod
{
    KP_Tick = -1,
    KP_Minute1 = 1,
    KP_Minute5 = 5,
    KP_Minute15 = 15,
    KP_Minute60 = 60,
    KP_DAY = 0,
    KP_Week = 7,
    KP_Month = 28
} WTSKlinePeriod;

typedef struct _Contract
{
    std::string	m_strCode{};
    std::string	m_strExchg{};
    std::string	m_strName{};
    std::string	m_strProduct{};

    uint32_t	m_maxMktQty{};
    uint32_t	m_maxLmtQty{};
    uint32_t	m_minMktQty{};
    uint32_t	m_minLmtQty{};

    uint32_t	m_uOpenDate{};
    uint32_t	m_uExpireDate{};

    double		m_dLongMarginRatio{};
    double		m_dShortMarginRatio{};

    uint32_t	m_uPrecision{};
    OptionType	m_optType{};
    std::string m_strUnderlying{};
    double		m_strikePrice{};
    double      m_priceTick{};
    int         m_iVolumeMultiple{};
    double		m_dUnderlyingScale{};
} Contract;
typedef std::map<std::string, Contract> ContractMap;

typedef struct _Commodity
{
    std::string	m_strName{};
    std::string	m_strExchg{};
    std::string	m_strProduct{};
    std::string	m_strCurrency{};
    std::string m_strSession{};
    std::string m_strHoliday{};

    uint32_t	m_uVolScale{};
    double		m_fPriceTick{};
    uint32_t	m_uPrecision{};

    ContractCategory	m_ccCategory{};
    CoverMode			m_coverMode{};
    PriceMode			m_priceMode{};
    TradingMode			m_tradeMode{};

} Commodity;
typedef std::map<std::string, Commodity> CommodityMap;
typedef std::map<std::string, std::string> StringMap;
//By Wesley @ 2021.12.31
//新的结构体，全部改成8字节对齐的方式
#pragma pack(push, 8)

struct WTSBarStruct
{
public:
	WTSBarStruct()
	{
		memset(this, 0, sizeof(WTSBarStruct));
	}
	uint32_t	date;		//日期
	uint32_t	reserve_;	//占位符
	uint64_t	time;		//时间
	double		open;		//开
	double		high;		//高
	double		low;		//低
	double		close;		//收
	double		settle;		//结算
	double		money;		//成交金额
	double		vol;		//成交量
	union
	{
		double		hold;	//总持
		double		bid;	//买价，期权专用，主要期权spread比较大，By Wseley @ 2023.05.04
	};
	union
	{
		double		add;	//增仓
		double		ask;	//卖价，期权专用，主要期权spread比较大，By Wseley @ 2023.05.04
	};
};

struct WTSTickStruct
{
	char		exchg[MAX_EXCHANGE_LENGTH];
	char		code[MAX_INSTRUMENT_LENGTH];

	double		price;				//最新价
	double		open;				//开盘价
	double		high;				//最高价
	double		low;				//最低价
	double		settle_price;		//结算价

	double		upper_limit;		//涨停价
	double		lower_limit;		//跌停价

	double		total_volume;		//总成交量
	double		volume;				//成交量
	double		total_turnover;		//总成交额
	double		turn_over;			//成交额
	double		open_interest;		//总持
	double		diff_interest;		//增仓

	uint32_t	trading_date;		//交易日,如20140327
	uint32_t	action_date;		//自然日期,如20140327
	uint32_t	action_time;		//发生时间,精确到毫秒,如105932000
	uint32_t	reserve_;			//占位符

	double		pre_close;			//昨收价
	double		pre_settle;			//昨结算
	double		pre_interest;		//上日总持

	double		bid_prices[10];		//委买价格
	double		ask_prices[10];		//委卖价格
	double		bid_qty[10];		//委买量
	double		ask_qty[10];		//委卖量
	WTSTickStruct()
	{
		memset(this, 0, sizeof(WTSTickStruct));
	}
};

#pragma pack(pop)


class CTrReceiver
{
public:
    virtual void OnReceiveData(TrResponseID rId, void* wParam, void* lParam) = 0;
};